package com.shensi.adminCollect.service.markert.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.shensi.adminCollect.common.exception.CustomException;
import com.shensi.adminCollect.domain.coupon.vo.CouponIssueInfoVO;
import com.shensi.adminCollect.service.coupon.ICouponIssueService;
import com.shensi.adminCollect.domain.markert.SaleActivity;
import com.shensi.adminCollect.domain.markert.SaleActivityCouponRel;
import com.shensi.adminCollect.domain.markert.req.SaleActivityQueryReq;
import com.shensi.adminCollect.domain.markert.vo.SaleActivityVO;
import com.shensi.adminCollect.mapper.markert.SaleActivityMapper;
import com.shensi.adminCollect.service.markert.ISaleActivityCouponRelService;
import com.shensi.adminCollect.service.markert.ISaleActivityService;
import com.shensi.common.core.domain.entity.SysUser;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @date 2023/12/20
 */


@Service
public class SaleActivityServiceImpl extends ServiceImpl<SaleActivityMapper, SaleActivity> implements ISaleActivityService {

    private static final Logger log = LoggerFactory.getLogger(SaleActivityServiceImpl.class);

    @Autowired
    private ISaleActivityCouponRelService saleActivityCouponRelService;
    @Autowired
    private ICouponIssueService couponIssueService;

    @Override
    @Transactional
    public void insert(SaleActivityVO saleActivityVO) {
        SaleActivity saleActivity = new SaleActivity();
        BeanUtils.copyProperties(saleActivityVO,saleActivity);
        save(saleActivity);
        saveActivityCoupons(saleActivity,saleActivityVO.getCouponList());
    }

    @Override
    @Transactional
    public void update(SaleActivityVO saleActivityVO) {
        SaleActivity saleActivity = new SaleActivity();
        BeanUtils.copyProperties(saleActivityVO,saleActivity);
        updateById(saleActivity);
        LambdaQueryWrapper<SaleActivityCouponRel> qw = Wrappers.lambdaQuery(SaleActivityCouponRel.class);
        qw.eq(SaleActivityCouponRel::getSaleActivityId,saleActivity.getId());
        saleActivityCouponRelService.remove(qw);
        saveActivityCoupons(saleActivity,saleActivityVO.getCouponList());
    }

    @Override
    @Transactional
    public void insertOrUpdate(SaleActivityVO saleActivityVO) {
        LoginUser user = SecurityUtils.getLoginUser();
        SysUser sysUser = user.getUser();
        if (sysUser.getEnterpriseId() == null) {
            throw new CustomException("油企id为空");
        }
        saleActivityVO.setEnterpriseId(sysUser.getEnterpriseId());
        saleActivityVO.setStock(saleActivityVO.getOriginStock());
        saleActivityVO.setDelFlg(null);
        if (saleActivityVO.getId() == null) {
            saleActivityVO.setCreateBy(sysUser.getUserId().toString());
            saleActivityVO.setCreateTime(LocalDateTime.now());
            insert(saleActivityVO);
        }else{
            saleActivityVO.setUpdateBy(sysUser.getUserId().toString());
            saleActivityVO.setUpdateTime(LocalDateTime.now());
            update(saleActivityVO);
        }
    }

    @Override
    public SaleActivityVO getSaleActivityVOById(Long id) {
        SaleActivity saleActivity = getById(id);
        if (saleActivity == null) {
            return null;
        }
        SaleActivityVO saleActivityVO = new SaleActivityVO();
        BeanUtils.copyProperties(saleActivity,saleActivityVO);
        LambdaQueryWrapper<SaleActivityCouponRel> qw = Wrappers.lambdaQuery(SaleActivityCouponRel.class);
        qw.eq(SaleActivityCouponRel::getSaleActivityId,saleActivity.getId());
        List<SaleActivityCouponRel> list = saleActivityCouponRelService.list(qw);
        ArrayList<CouponIssueInfoVO> couponList = Lists.newArrayListWithCapacity(list.size());
        for (SaleActivityCouponRel activityCouponRel : list) {
            CouponIssueInfoVO issueInfoVO = couponIssueService.selectCouponIssueByIdWithInfo(activityCouponRel.getCouponId());
            if (issueInfoVO != null) {
                couponList.add(issueInfoVO);
            }
        }
        saleActivityVO.setCouponList(couponList);
        return saleActivityVO;
    }

    @Override
    public List<SaleActivity> queryList(SaleActivityQueryReq queryReq) {
        LoginUser user = SecurityUtils.getLoginUser();
        SysUser sysUser = user.getUser();
        queryReq.setEnterpriseId(sysUser.getEnterpriseId());
        LambdaQueryWrapper<SaleActivity> qw = Wrappers.lambdaQuery(queryReq);
        if (queryReq.getActivityBeginTime()!=null && queryReq.getActivityEndTime() != null) {
            qw.gt(SaleActivity::getStartTime,queryReq.getActivityBeginTime())
                    .lt(SaleActivity::getStartTime,queryReq.getActivityEndTime());
        }
        qw.orderByDesc(SaleActivity::getId);
        List<SaleActivity> list = list(qw);
        LocalDateTime now = LocalDateTime.now();
        for (SaleActivity activity : list) {
            if (activity.getStartTime() != null && activity.getEndTime() != null) {
                if (now.isBefore(activity.getStartTime())) {
                    activity.setActivityStatus(0);
                }else if(now.isAfter(activity.getEndTime())){
                    activity.setActivityStatus(2);
                }else{
                    activity.setActivityStatus(1);
                }
            }
        }
        return list;
    }

    @Override
    public void switchEnable(Long id) {
        SaleActivity saleActivity = getById(id);
        if (saleActivity == null) {
            return;
        }
        Integer enable = saleActivity.getActivityEnable();
        LambdaUpdateWrapper<SaleActivity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(SaleActivity::getActivityEnable,enable == 0 ? 1 : 0).eq(SaleActivity::getId,id);
        update(wrapper);
    }

    private void saveActivityCoupons(SaleActivity saleActivity,List<CouponIssueInfoVO> couponList){
        if (couponList == null || couponList.isEmpty()) {
            return;
        }
        List<SaleActivityCouponRel> activityCouponRels = couponList.stream().map(couponIssue -> {
            SaleActivityCouponRel activityCouponRel = new SaleActivityCouponRel();
            activityCouponRel.setSaleActivityId(saleActivity.getId());
            activityCouponRel.setCouponId(couponIssue.getId());
            return activityCouponRel;
        }).collect(Collectors.toList());
        saleActivityCouponRelService.saveBatch(activityCouponRels);
    }

}
